package game.server;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSocketServer {
	
	public static void main(String[] args) {
		recive();
	}

	private static void recive() {
		System.out.println("接收端启动");
		
		try {
			DatagramSocket socket = new DatagramSocket(9001, InetAddress.getByName("localhost"));
			// 接收数据的buf数组并指定大小
			byte[] buf = new byte[1023];
			// 创建接收数据报，存储在buf中
			DatagramPacket packet = new DatagramPacket(buf, buf.length);
			socket.receive(packet);
			
			byte[] data = packet.getData();
			InetAddress address = packet.getAddress();
			
			System.out.println("接收的数据：" + new String(data));
			System.out.println("Client IP: " + address.toString());
			System.out.println("Client Port: " + packet.getPort());
			
			byte[] resp = "服务端接收完毕。".getBytes();
			DatagramPacket sendPacket = new DatagramPacket(resp, resp.length,packet.getSocketAddress());
			socket.send(sendPacket);
			socket.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

}
